<template>
    <div></div>
</template>
<script>
export default {
    name: "OtherLogin",
    layout: "layout-empty",
    data () {
        return {};
    },
    methods: {
        setLoading () {
            const loading = this.$loading({
                lock: true, //同v-loading的修饰符
                text: "loading...", //加载文案
                backgroundColor: "rgba(55,55,55,0.4)", //背景色
                spinner: "el-icon-loading", //加载图标
                target: document.querySelector("body"), //loading需要覆盖的DOM节点，默认为body
            });
            return loading;
        },
        async doLogin () {
            let loading = this.setLoading();
            let resdata = {
                state: this.$route.query.state,
                code: this.$route.query.code,
            };

            // 推广码
            try {
                const config = JSON.parse(localStorage.getItem("CONFIG") || '{}');
                const nowtime = new Date().getTime();
                if (config.distributor && config.distributor.expire >= nowtime) {
                    resdata.identifier_code = config.distributor.code;
                }
            } catch (err) {

            }


            let res = await this.$http("getGoogleLoginCallback", resdata);
            if (res.code == 0 && res.data) {

                this.$store.dispatch("doLogin", res.data.token).then(() => {

                    if (res.data.is_new) {
                        // 清除推广码
                        try {
                            const config = JSON.parse(localStorage.getItem("CONFIG") || '{}');
                            delete config.distributor;
                            localStorage.setItem("CONFIG", JSON.stringify(config));
                        } catch (err) {

                        }

                        // 新用户
                        this.$gmeRepore('register').then(() => {
                            window.close();
                            loading.close();
                        })
                    } else {
                        window.close();
                        loading.close();
                    }

                }).catch(err => {
                    window.close();
                    loading.close();
                })

            } else {
                loading.close();
                this.$uxMessage.warning(res.msg);
            }
        },
    },
    mounted () {
        if (this.$route.query.state && this.$route.query.code) {
            this.doLogin();
        } else {
            this.$router.push("/document/login-error");
        }
    },
};
</script>
<style scoped></style>
